package com.hanma.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.hanma.bean.Category;
import com.hanma.bean.News;
import com.hanma.bean.Newsname;
import com.hanma.service.CategoryService;
import com.hanma.service.NewsService;
import com.hanma.service.impl.CategoryServiceImpl;
import com.hanma.service.impl.NewsServiceImpl;
import com.mysql.cj.xdevapi.JsonArray;

@WebServlet({"/newsadd.do","/newsaddAction.do","/newslist.do","/getCate.do","/newsupdate.do","/newsupdateAction.do","/newsdelete.do"})
public class NewsServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//后端新闻处理
		String path = req.getServletPath();
		req.setCharacterEncoding("utf-8");
		NewsService newsService = new NewsServiceImpl();
		CategoryService cateService = new CategoryServiceImpl();
		//添加新闻
		if(path.equals("/newsadd.do")) {
			Integer pid = 0;
			List<Category> cateList = cateService.findAll(pid);
			req.setAttribute("cateList", cateList);
			req.getRequestDispatcher("manage/news/add.jsp").forward(req, resp);
			
		}else if(path.equals("/getCate.do")) {
			resp.setCharacterEncoding("utf-8");
			Integer cateId = Integer.parseInt(req.getParameter("cateId"));
			List<Category> seCateList = cateService.findAll(cateId);
			JSONArray jsonArray = new JSONArray(seCateList);
			resp.getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
		}else if(path.equals("/newsaddAction.do")) {
			String title = req.getParameter("name");
			String content = req.getParameter("content");
			Integer cate_id = Integer.parseInt(req.getParameter("pid1"));
			Integer seCate_id = Integer.parseInt(req.getParameter("pid2"));
			News news = new News(title,cate_id,seCate_id,content);
			newsService.insert(news);
			resp.sendRedirect("newslist.do");
		}
		//新闻列表
		else if(path.equals("/newslist.do")) {
			List<News> newsList = null;
			Integer seCateId = 0;
			if(req.getParameter("seCateId") != null){
				seCateId = Integer.parseInt(req.getParameter("seCateId"));
				newsList = newsService.findAllTwo(seCateId);
			}else{
				newsList = newsService.findAllByState();
			}
			Integer currentPage = 1;
			if(req.getParameter("currentPage") != null) {
				currentPage = Integer.parseInt(req.getParameter("currentPage"));
			}
			Integer size = 2;
			Integer totalPage = newsList.size() % 2 == 0 ? newsList.size() / 2 : (newsList.size() / 2 + 1);
			Integer prePage = currentPage - 1 <= 0 ? 1 : currentPage - 1;
			Integer nextPage = currentPage + 1 >= totalPage ? totalPage : currentPage + 1;
			req.setAttribute("totalPage", totalPage);
			req.setAttribute("currentPage", currentPage);
			req.setAttribute("prePage", prePage);
			req.setAttribute("nextPage", nextPage);
			Integer start = (currentPage - 1) * size;
			if(seCateId != 0){
				newsList = newsService.findAllTwo(seCateId, start, size);
			}else{
				newsList = newsService.findAllByState(start, size);
			}
			System.out.println("newsList"+newsList);
			List<Newsname> newsList1 = new ArrayList();
			for(News news : newsList) {
				String title = news.getTitle();
				String cate_name = cateService.find(news.getCateId()).getName();
				String se_cate_name = cateService.find(news.getSeCateId()).getName();
				Newsname newsname = new Newsname(news.getId(), title,cate_name,se_cate_name);
				newsList1.add(newsname);
			}
			System.out.println("newsList1"+newsList1);
			System.out.println("seCateId"+seCateId);
			req.setAttribute("seCateId", seCateId);
			req.setAttribute("newsList1", newsList1);
			req.getRequestDispatcher("manage/news/list.jsp").forward(req, resp);
		} else if (path.equals("/newsupdate.do")) {
			Integer pid = 0;
			List<Category> cateList = cateService.findAll(pid);
			Integer id = Integer.parseInt(req.getParameter("id"));
			News news = newsService.find(id);
			String se_cate_name =cateService.find(news.getSeCateId()).getName();
			req.setAttribute("se_cate_name", se_cate_name);
			req.setAttribute("news",news);
			req.setAttribute("cateList", cateList);
			req.getRequestDispatcher("manage/news/update.jsp").forward(req, resp);
		} else if (path.equals("/newsupdateAction.do")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			String title = req.getParameter("name");
			String content = req.getParameter("content");
			Integer cate_id = Integer.parseInt(req.getParameter("pid1"));
			Integer seCate_id = Integer.parseInt(req.getParameter("pid2"));
			News news = new News(id,title,cate_id,seCate_id,content);
			newsService.update(news);
			resp.sendRedirect("newslist.do");
		} else if (path.equals("/newsdelete.do")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			newsService.delete(id);
			resp.sendRedirect("newslist.do");
		}
	}
	
}
